package top.kaoshanji.r3ch11a;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 代码清单-r3ch11a-367页a：演示Spring对有状态Bean改造成线程安全的示例
 * @author kaoshanji
 * ^_^
 * create time 2025/5/8 19:10
 */
public class TopicDao {

    // 使用ThreadLocal保存Connection变量
    private static ThreadLocal<Connection> connThreadLocal = new ThreadLocal<>();

    public static Connection getConnection() {

        // 如果connThreadLocal没有本线程对应的Connection，则创建一个新的Connection
        // 并将其保存到线程本地变量中
        if (connThreadLocal.get() == null) {
            Connection connection = null;
            connThreadLocal.set(connection);
            return connection;
        } else {
            // 直接返回线程本地变量
            return connThreadLocal.get();
        }
    }

    public void addTopic() throws SQLException {
        // 从ThreadLocal中获取线程对应的Connection
        Statement statement = getConnection().createStatement();
    }



}
